import os
import os.path as osp
import shutil


def copyAllFiletoPath(old_path,to_path,to_path_file):
    try:
      shutil.copy(old_path,to_path)
    except:
      createFile(to_path_file)
      copy(old_path,to_path_file)


def copy(src_path,target_path):
    if os.path.isdir(src_path):
        filelist1=os.listdir(src_path)
        for file in filelist1:
            path=os.path.join(src_path,file)
            if os.path.isdir(path):
                path1=os.path.join(target_path,file)
                print(path1)
                os.mkdir(path1)
                copy(path,path1)
            else:
                with open(path,"rb") as f1:
                    container=f1.read()
                    path2=os.path.join(target_path,file)
                    with open(path2,"wb") as f2:
                        f2.write(container)
        else:
            print("复制完成")



def list_file_name(file_dir):   
    file_list = os.listdir(file_dir) #.代表根目录下
    print("file_list列表 =", file_list)
    print("---------------------------循环列表输出")
    # for item in file_list:
    #   print(item)
    return file_list

def createFile(to_path):
   if not os.path.isdir(to_path):
      os.makedirs(to_path)    
        # result = os.listdir(file_dir)
        # print(result)

        # for root, dirs, files in result:  
        #     # print(root) #当前目录路径  
        #     print("开始记录")
        #     print(dirs) #当前路径下所有子目录  
        #     print(files) #当前路径下所有非目录子文件
        #     # break
